home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Booting Gallery / Booting Gallery (source) / Sources / Blasteroids / BlasteroidsGame.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  3.3 KB  |  173 lines  |  [TEXT/CWIE]

  1. /***
  2.  *     Created by Bill Hubauer on Fri, Jun 21, 1996 @ 2:25 AM.
  3.  *
  4.  ***/
  5.  
  6.  
  7. #ifndef __BlasteroidsGame_H__
  8. #include "BlasteroidsGame.h"
  9. #endif
  10. #include "BlasteroidsShipSprite.h"
  11.  
  12.  
  13. CSprite*    CBlasteroidsGame::MakeExtensionSprite(GWorldPtr image,RgnHandle mask) //Override
  14. {
  15.     return new CBlasteroidTargetSprite(GetWorld(), this, image, mask);
  16. }
  17.  
  18.  
  19. OSErr        CBlasteroidsGame::Initialize()//Override
  20. {
  21.     OSErr        err = noErr;
  22.     
  23.     err = inherited::Initialize();
  24.     if(err == noErr){
  25.         new CBlasteroidsShipSprite(GetWorld(), this);
  26.         
  27.         PlaySound(5000,true);
  28.     }
  29.     
  30.     return err;
  31. }
  32.  
  33.  
  34. CBlasteroidsGame::CBlasteroidsGame()
  35.     :    CSpriteGame(5000)
  36. {
  37. }
  38.  
  39.  
  40. CBlasteroidsGame::~CBlasteroidsGame()//Override
  41. {
  42. }
  43.  
  44.  
  45. static short Abs(short value)
  46. {
  47.     if (value < 0)
  48.         return -value;
  49.     else
  50.         return value;
  51. }
  52.  
  53.  
  54. static short RandomInRange(short low, short hi)
  55. {
  56.     if (hi == low)
  57.         return low;
  58.         
  59.     if (hi < low)
  60.     {
  61.         short temp = hi;
  62.         hi = low;
  63.         low = temp;
  64.     }
  65.     
  66.     return (Abs(Random()) % (hi - low + 1)) + low;
  67. }
  68.  
  69.  
  70. void CBlasteroidsGame::DrawBackground(const Rect& inBounds)
  71. {
  72.     RGBColor pixel = {-1, -1, -1};
  73.     for (short i=1; i<500; i++)
  74.     {
  75.         SetCPixel(RandomInRange(inBounds.left, inBounds.right), 
  76.             RandomInRange(inBounds.top, inBounds.bottom), &pixel);
  77.     }
  78. }
  79.  
  80.  
  81. static short CalcBlasteroidTargetStartTop(CSpriteWorld* world)
  82. {
  83.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  84.     
  85.     return RandomInRange(bounds.top+10, bounds.bottom-32-10);
  86. }
  87.  
  88.  
  89. static short CalcBlasteroidTargetStartLeft(CSpriteWorld* world)
  90. {
  91.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  92.     
  93.     return RandomInRange(bounds.left+10, bounds.right-32-10);
  94. }
  95.  
  96.  
  97. CBlasteroidTargetSprite::CBlasteroidTargetSprite(CSpriteWorld* world,CSpriteGame* game,GWorldPtr image,
  98.                         RgnHandle mask)
  99.     : CGameSprite(world, game, 0, image, 
  100.         0, 0, mask)
  101. {
  102.     const Rect&        bounds = *(world->GetSpriteCanvas()->GetBounds());
  103.  
  104.     switch (RandomInRange(1, 3))
  105.     {
  106.         case 0:
  107.             MoveTo(RandomInRange(bounds.left, bounds.left+100), 
  108.                 RandomInRange(bounds.top, bounds.top+100));
  109.             break;
  110.         case 1:
  111.             MoveTo(RandomInRange(bounds.right-100, bounds.right), 
  112.                 RandomInRange(bounds.top, bounds.top+100));
  113.             break;
  114.         case 2:
  115.             MoveTo(RandomInRange(bounds.left, bounds.left+100), 
  116.                 RandomInRange(bounds.bottom-100, bounds.bottom));
  117.             break;
  118.         default:
  119.             MoveTo(RandomInRange(bounds.right-100, bounds.right), 
  120.                 RandomInRange(bounds.bottom-100, bounds.bottom));
  121.             break;
  122.     }
  123.     
  124.     fDeltaH = RandomInRange(-10, 10);
  125.     fDeltaV = RandomInRange(-10, 10);
  126. }
  127.  
  128.  
  129. CBlasteroidTargetSprite::~CBlasteroidTargetSprite()
  130. {
  131. }
  132.  
  133.  
  134. void    CBlasteroidTargetSprite::UpdatePosition() //Override
  135. {
  136.     MoveBy(fDeltaH,fDeltaV);
  137.     
  138.     if (fLocation.left < GetGameBounds()->left)
  139.         MoveToH(GetGameBounds()->right);
  140.     if (fLocation.left > GetGameBounds()->right)
  141.         MoveToH(GetGameBounds()->left);
  142.     if (fLocation.top < GetGameBounds()->top)
  143.         MoveToV(GetGameBounds()->bottom);
  144.     if (fLocation.top > GetGameBounds()->bottom)
  145.         MoveToV(GetGameBounds()->top);
  146. }
  147.  
  148.  
  149. Boolean    CBlasteroidTargetSprite::WasHitBy(CSprite* hitByThis)
  150. {
  151.     CBlasteroidsBulletSprite* bullet = dynamic_cast<CBlasteroidsBulletSprite*>(hitByThis);
  152.     if (bullet != nil)
  153.     {
  154.         PlaySound(5002);
  155.         delete bullet;
  156.         delete this;
  157.         return false;
  158.     }
  159.     
  160.     CBlasteroidsShipSprite* ship = dynamic_cast<CBlasteroidsShipSprite*>(hitByThis);
  161.     if (ship != nil)
  162.     {
  163.         PlaySound(5003);
  164.         //delete ship;
  165.         return false;
  166.     }
  167.  
  168.     return true;
  169. }
  170.  
  171.  
  172.  
  173.